package cn.bluedot.framework.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {
	
	private static Properties prop = null;
	
	static {
		InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
		prop = new Properties();
		try {
			prop.load(in);
		} catch (IOException e) {
			System.err.println("db.properties 加载失败");
		}
		
		try {
			Class.forName(prop.getProperty("jdbc.driver"));
		} catch (ClassNotFoundException e) {
			System.err.println("数据库驱动加载失败");
		}
	}
    
	public static Connection getConnection() {
		
		Connection connection = null;
		
		try {
			String url = prop.getProperty("jdbc.url");
			String username = prop.getProperty("jdbc.username");
			String password = prop.getProperty("jdbc.password");
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			System.err.println("数据库连接失败");
			e.printStackTrace();
		}
		return connection;
	}
}
